/**
 * 
 */

var ATTACHMENT_COMP_FOR_NEW_PO = "poNum";
var ATTACHMENT_COMP_FOR_OPEN_PO = "poNumShow";
var ATTACHMENT_ID = "";


var attachmentctr = 0;

function assignAttachmentComponent(domElementName,idElement){
	YAHOO.util.Dom.insertAfter ( "attachmentComponent",domElementName) ;
	//jQuery("#attachmentComponent").insertAfter(jQuery("#"+domElementName));
	dwr.util.removeAllRows("poattachments_tbody", {
		filter : function(tr) {
			return (tr.id != "poattachments_pattern");
		}
	});
	attachmentctr = 0;
	ATTACHMENT_ID = "";
	ATTACHMENT_ID = idElement;
	document.getElementById('attachmentComponent').style.display='block';
}

function uploadAttachment() {
	var filename = dwr.util.getValue('uploadFile');
	document.getElementById("attachmentId").value = dwr.util.getValue(ATTACHMENT_ID);
	if(isEmptyString(document.getElementById("attachmentId").value)){
		showusrmessage("Please Select a document!!");
		dwr.util.setValue('uploadFile',"");
		return;
	}
	// which tells Connection Manager this is a file upload form
	YAHOO.util.Connect.setForm('addForm', true);
	//$("attachment_Throbber").style.display = "inline";
	var uploadHandler = {
		upload : function(o) {
		var ids = attachmentctr + "";
		dwr.util.cloneNode('poattachments_pattern', {
			idSuffix : ids
		});
		
		//$("attachment_Throbber").style.display = "none";
		dwr.util.setValue('poaattach_id'+ids,o.responseText.split(":")[0]+":"+o.responseText.split(":")[3]);
		
		//a.getId()+":"+a.getUniqueName()+":"+a.getDescription()+":"+item.getName()
		
		jQuery("#poattachments_link"+ids).attr("href",'javascript:openAttachmentPanel("'+o.responseText.split(":")[0]+'");');
		document.getElementById("poattachments_link"+ids).innerHTML = o.responseText.split(":")[3];
		attachmentctr++;
		jQuery("#poattachments_pattern"+ids).addClass('show');
		dwr.util.setValue('uploadFile',"");
		
		}
	};
	try{
		YAHOO.util.Connect.asyncRequest('POST', '/addAttachment/', uploadHandler);
	}catch(ex){
		console.log(ex);
	}
}

function getAttachmentDetails(backendId){
	var dwrCallMetaData = {
			callback : success_getAttachmentDetails,
			exceptionHandler : null,
			timeout : 100000,
			errorHandler : null
		};

	
	DWR_PurchaseOrders.getAttachmentDetails(backendId,dwrCallMetaData);
}

function success_getAttachmentDetails(response){
	var shref = "http://"+window.location.host + response.objRespOrError.uniqueName; 
	window.open(shref, "popup", "width=400,height=400");
}

function openAttachmentPanel(id){
	getAttachmentDetails(id);
}

function displayPrevAttachment(backendIds){
	for(var i =0;i<backendIds.length;i++){
		
		var attachmentObj = backendIds[i];
		var attachmentObjArray = attachmentObj.split(":");
		var sHTML = "<input checked type='checkbox' disabled>&nbsp;&nbsp;<a href=\"javascript:openAttachmentPanel('"+attachmentObjArray[0]+"');\">"+ attachmentObjArray[1]+"</a>"
				
		jQuery('#po_existingAttachmentLink').append(sHTML);
		
	}
}



function assignReadOnlyAttachmentComponent(domElementName,arrayofIds){
	if(isEmptyString(arrayofIds)){
		return;
	}
	displayPrevAttachment(arrayofIds);
	
}



